import {validate} from '../../../common/js/validate'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar: '/static/images/personal/index/avatar.png',
    finish: false,
    submitDisable: true,
    mobileDisable: true,
    userInfo: {},
    phoneNumber: '',
    showPopup: false,
    msg: '发送验证码',
    count: 60,
    timer: false,
    send: false,
    tabNav:0,
    hospInfo:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserInfo()
    this.setData({
      tabNav:wx.getStorageSync('tabNav')
    })
    // this.getList()
  },

  /**
   * 获取用户信息
   */
  getUserInfo() {
    const that = this
    wx.showLoading({
      title: '',
      icon:'none'
    })
    app.getUserInfo().then(res=>{
      that.setData({
        userInfo:res
      })

      if(this.data.tabNav==1){
        app.getHost(that.data.userInfo.hospitalId).then(res => {
          that.setData({
            hospInfo: res,
          })
          wx.hideLoading()
        })
      }else{
        wx.hideLoading()
      }
    })
    
  },
  bindimgs:function(){
    wx.navigateTo({
      url: '/pagesA/usericondiy/index?userInfo='+JSON.stringify(this.data.userInfo),
    })
  },
  bindinputmobile: function (e) {
    this.setData({
      mobileDisable: !e.detail.value,
      phoneNumber: e.detail.value,
      mobileErr: '',
      codeErr: ''
    })
    this.changeSubmitStatus()
  },
  bindinputcode: function (e) {
    this.setData({
      code: e.detail.value
    })
    this.changeSubmitStatus()
  },
  changeSubmitStatus() {
    if (this.data.phoneNumber && this.data.code) {
      this.setData({
        submitDisable: false
      })
    } else {
      this.setData({
        submitDisable: true
      })
    }
  },
  // getList:function(){
  //   let that = this
  //   wx.request({
  //     url: app.globalData.serverUrl+'/medical/staffInfo',
  //     method:'GET',
  //     header:{
  //       'token':wx.getStorageSync('token')
  //     },
  //     success:(res=>{
  //       let resData= res.data
  //       if(resData.code==200){
  //         console.log(resData.data);
  //         that.setData({
  //           userInfo:resData.data.staffInfo
  //         })
  //       }
  //     })
  //   })
  // },
  //退出
  OutLogin() {
    
    wx.removeStorage({
      key: 'token',
      success(res) {
        wx.reLaunch({
          url: '/pages/login/login',
        })
      }
    })

  },
  /** 
   * 手机号弹窗
   */
  showMobileForm() {
    this.setData({
      showPopup: true
    })
  },
  /**
   * 修改手机号发送验证码
   */
  tapSendCode: function () {
    const self = this
    if (!validate(this, 'phoneNumber')) {
      return
    }
    wx.showLoading({
      mask: true
    })
    if (this.data.send) {
      return
    }
    self.setData({
      send: true,
      mobileDisable: true
    })
    wx.request({
      url: app.globalData.serverUrl + '/user/code/' + self.data.phoneNumber,
      method: 'GET',
      success(res) {
        wx.hideLoading()
        self.timer = setInterval(() => {
          let count = self.data.count - 1
          let msg = count + '秒后重新发送'
          if (count === 0) {
            clearInterval(self.timer)
            msg = "重新发送"
            self.setData({
              send: false,
              count: 60,
              msg: msg,
              mobileDisable: false
            })
            return
          }
          self.setData({
            count: count,
            msg: msg
          })
        }, 1000)
      },
      error(data) {
        self.setData({
          send: false
        })
        app.showMsg(data.msg)
      }
    })
  },
  /**
   * 关闭手机号弹窗
   */
  hideMobileForm() {
    this.setData({
      showPopup: false
    })
  },
  bindtapFinish() {
    let that = this
    let data = {
      code: that.data.code,
      phoneNumber: that.data.phoneNumber
    }
    wx.request({
      url: app.globalData.serverUrl +(that.data.tabNav==0?'/customer/change/phone':'/medical/change/phone') ,
      method: 'POST',
      data,
      header:{
        'token':wx.getStorageSync('token')
      },
      success:(res=>{
        console.log(res);
        let resp = res.data
        if(resp.code==200){
          wx.showToast({
            title: resp.message,
            mask:true,
            success:(rescode=>{
              wx.clearStorageSync()
              wx.reLaunch({
                url: '/pages/login/login',
              })
            })
          })
        }else{
          app.showMsg(resp.message)
        }
      })
    })
  }
})